<?php  namespace Admin\Controller;

use Common\Controller\BaseController;
use Common\Model\CategoryModel;
use Org\Util\Arr;


class  CategoryController extends  BaseController{

    /**
     * 列表模板首页
     */
    public function index()
    {
        $data = m('category')->select();
        //测试树状结构
        $data = (new Arr())->tree($data,'cname');
        //dump($data);
        $this->assign('data',$data);
        $this->display();


    }

    /**
     * 添加顶级分类方法
     */
   public function addtop()
   {
       if($_POST)
       {
       $data = I('post.');
       $this->store( new CategoryModel(),$data,u('admin/category/index'));
       }
       $this->display();
   }

    /**
     * 添加子集方法
     */
    public function subset()
    {
        if(IS_POST)
        {
          $data =  I('post.');
          $this->store(new CategoryModel(),$data,u('admin/Category/index'));
        }
        $cid =  I('get.cid');
        $dd =  m('category')->find($cid);
        $this->assign('dd',$dd);

        $dda = m('type')->select();

        $this->assign('dda',$dda);
        $this->display();
    }

    /**
     * 编辑方法
     */
    public function edit()
    {
        $cid = I('get.cid');
       //dump($cid);
        if(IS_POST)
        {
         $data = I('post.');
         $data['cid'] =$cid;


         $this->store(new CategoryModel(),$data,u('admin/Category/index'));
        }
        //旧数据
        $edit = m('category')->find($cid);
        $this->assign('edit',$edit);
//         dump($edit);die;
        //找自己
       $res= (new CategoryModel())->getCateData();

        $this->assign('oldData',$res);

        $dda = m('type')->select();
        $this->assign('dda',$dda);
        $this->display();

    }



    /**
     * 删除数据
     */
    public function del()
    {
       $cid = I('get.cid');
      if((new CategoryModel())->del($cid)){
          $this->success('删除成功',u('admin/Category/index'));exit;
      }


    }
}
